package cn.asterly.cicada.web.controller;

import cn.asterly.cicada.common.model.DataTablesResult;
import cn.asterly.cicada.common.model.Result;
import cn.asterly.cicada.common.model.ResultStatus;
import cn.asterly.cicada.model.Menu;
import cn.asterly.cicada.service.impl.MenuServicelmpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ Author     ：likun
 * @ Date       ：Created in 14:40 2019-02-27
 * @ Description：菜单接口
 * @Version: $
 */
@RestController
@RequestMapping("/menus")
@Api(value = "MenusController",description = "后台管理系统左侧菜单栏")
public class MenuController implements BaseController<Menu>{

    @Autowired
    private MenuServicelmpl servicelmpl;

    private static Logger logger=Logger.getRootLogger();

    @Override
    @RequestMapping(value = {"/menuadd","/addmenu"},method = RequestMethod.POST)
    @ApiOperation(value = "新增菜单",httpMethod = "POST")
    public Result<Menu> addition(Menu menu) {

        if(servicelmpl.insertSelective(menu)){
            return ResultStatus.addsuccess();
        }
        return ResultStatus.adderror();
    }

    @Override
    @RequestMapping(value = "/editmenua",method = RequestMethod.POST)
    @ApiOperation(value = "修改或编辑菜单信息",httpMethod = "POST")
    public Result<Menu> editInfo(Menu menu) {
        if(servicelmpl.updateByPrimaryKeySelective(menu)){
            return ResultStatus.editsuccess();
        }
        return ResultStatus.editerror();
    }

    @Override
    @RequestMapping(value = "/delmenu/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id删除菜单",httpMethod = "GET")
    public Result delById(@PathVariable String id) {
        //将id转换成long类型数据
        long lid= 0;
        try {
            lid = (long)Integer.parseInt(id);
            if(servicelmpl.deleteByPrimaryKey(lid)){
                return ResultStatus.delsuccess();
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result(false,"id转换异常",5403);
        }

        return ResultStatus.delerror();
    }

    @Override
    @RequestMapping(value = "/selmenuid/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询指定菜单信息",httpMethod = "GET")
    public Result selById(@PathVariable String id) {
        long lid=(long) Integer.parseInt(id);
        Menu menu = servicelmpl.selectByPrimaryKey(lid);
        return new Result(true,"成功查询id="+id,5200,menu);
    }

    @Override
    @RequestMapping(value = "/selmenus",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有菜单",httpMethod = "GET")
    public DataTablesResult selByAll() {
        List<Menu> menus =servicelmpl.selByAll();
        return new DataTablesResult(true,"",5200,menus);
    }
}
